package com.sunny.l39;

public class Solution {
    private boolean res = true;
    public boolean isBalanced(TreeNode root) {
        dfs(root);
        return res;
    }

    public int dfs(TreeNode node){
        if(null == node || !res)
            return 0;
        int left = dfs(node.left);
        int right = dfs(node.right);
        if(Math.abs(left-right) > 1){
            res = false;
        }
        return Integer.max(left, right) + 1;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}
